package org.xtb.taxCalculator.service.impl;

import org.springframework.stereotype.Service;
import org.xtb.taxCalculator.service.TaxCalculatorService;

import javax.jws.WebService;

@Service
@WebService(serviceName =  "taxCalculatorService",
        targetNamespace ="http://service.taxCalculator.xtb.org/",
        endpointInterface ="org.xtb.taxCalculator.service.TaxCalculatorService")
public class TaxCalculatorServiceImpl implements TaxCalculatorService {
    @Override
    public double calculateTax(double income) {
        double tax = 0.0;
        double taxableIncome = income - calculateDeductions(income);

        if (taxableIncome <= 0) {
            return tax;
        }

        if (taxableIncome <= 36000) {
            tax = taxableIncome * 0.03;
        } else if (taxableIncome <= 144000) {
            tax = 36000 * 0.03 + (taxableIncome - 36000) * 0.1;
        } else if (taxableIncome <= 300000) {
            tax = 36000 * 0.03 + 108000 * 0.1 + (taxableIncome - 144000) * 0.2;
        } else if (taxableIncome <= 420000) {
            tax = 36000 * 0.03 + 108000 * 0.1 + 156000 * 0.2 + (taxableIncome - 300000) * 0.25;
        } else if (taxableIncome <= 660000) {
            tax = 36000 * 0.03 + 108000 * 0.1 + 156000 * 0.2 + 120000 * 0.25 + (taxableIncome - 420000) * 0.3;
        } else if (taxableIncome <= 960000) {
            tax = 36000 * 0.03 + 108000 * 0.1 + 156000 * 0.2 + 120000 * 0.25 + 240000 * 0.3 + (taxableIncome - 660000) * 0.35;
        } else {
            tax = 36000 * 0.03 + 108000 * 0.1 + 156000 * 0.2 + 120000 * 0.25 + 240000 * 0.3 + 300000 * 0.35 + (taxableIncome - 960000) * 0.45;
        }

        return tax;
    }

    private double calculateDeductions(double income) {
        double deductions = 0.0;

        double socialSecurity = calculateSocialSecurity(income);
        double housingFund = calculateHousingFund(income);
        double medicalInsurance = calculateMedicalInsurance(income);

        deductions = socialSecurity + housingFund + medicalInsurance;

        return deductions;
    }

    private double calculateSocialSecurity(double income) {
        // 假设社保扣除比例为10%
        double socialSecurityRate = 0.1;
        double socialSecurity = income * socialSecurityRate;
        return socialSecurity;
    }

    private double calculateHousingFund(double income) {
        // 假设公积金扣除比例为5%
        double housingFundRate = 0.05;
        double housingFund = income * housingFundRate;
        return housingFund;
    }

    private double calculateMedicalInsurance(double income) {
        // 假设医疗保险扣除比例为3%
        double medicalInsuranceRate = 0.03;
        double medicalInsurance = income * medicalInsuranceRate;
        return medicalInsurance;
    }
}
